/**
 * @(#)ApkConfigController.java 2016年12月1日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.dao.IBrushConfigTimerDAO;
import org.jsbd.boss.domian.channel.BrushConfigTimer;
import org.jsbd.boss.service.IBrushConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/brushConfigTimer")
public class BrushConfigTimerController {
	public static final Logger LOGGER = LoggerFactory.getLogger(BrushConfigTimerController.class);

	@Resource
	private IBrushConfigTimerDAO brushConfigTimerDAO;
	@Resource
	private IBrushConfigService brushConfigService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list() {
		return "/brushConfigTimer/configList";
	}

	@RequestMapping(value = "configList", params = "json")
	@ResponseBody
	public Map<String, Object> configPageList(BrushConfigTimer config) {

		Integer isExec = config.getIsExec();
		if (isExec != null && isExec == -1) {
			config.setIsExec(null);
		}
		Map<String, Object> map = new HashMap<>();
		String startDay = config.getStartDay();
		String endDay = config.getEndDay();
		if(StringUtils.isNotBlank(startDay)){
			startDay = startDay.replace("-", "").substring(0,8);
		}else{
			startDay = null;
		}
		if(StringUtils.isNotBlank(endDay)){
			endDay = endDay.replace("-", "").substring(0,8);
		}else{
			endDay = null;
		}
		config.setStartDay(startDay);
		config.setEndDay(endDay);

		String brushName = config.getBrushName();
		if (StringUtils.isNotBlank(brushName)) {
			String[] marks = brushName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : marks) {
				keywords.add(keyword);
			}
			config.setKeywords(keywords);
		}

		List<BrushConfigTimer> list = brushConfigTimerDAO.selectList(config);
		Integer total = brushConfigTimerDAO.countConig(config);
		map.put("rows", list);
		map.put("total", total);
		return map;
	}

	@RequestMapping(value = "addConfig", method = RequestMethod.GET)
	public String add() {
		return "/brushConfigTimer/add";
	}

	@RequestMapping(value = "{id}/configUpdate")
	public String configUpdate(BrushConfigTimer config, Model model) {
		config = brushConfigTimerDAO.selectConfig(config);
		String url = config.getUrl();
		if(StringUtils.isNotBlank(url)){
			url=url.trim().replace("\n","").replace("\r","");
			config.setUrl(url);
		}
		model.addAttribute("config", config);
		return "brushConfigTimer/updateConfig";
	}

	@RequestMapping(value = "/saveConfig")
	public void saveConfig(BrushConfigTimer config, HttpServletResponse response) {
		boolean flag = this.brushConfigService.checkBrushUrl(config.getBrushId(), config.getUrl());
    	if (!flag) {
    	   ResponseUtils.responseCustomMsg(response, "链接填写错误");
		} else {
			String execTime = config.getExecTime();
			config.setExecDay(execTime.substring(0, 10).replace("-", ""));
			brushConfigTimerDAO.insertConfig(config);
			ResponseUtils.responseSuccess(response);	
		}
	}

	@RequestMapping(value = "/updateConfig")
	public void updateConfig(BrushConfigTimer config, HttpServletResponse response) {
		boolean flag = this.brushConfigService.checkBrushUrl(config.getBrushId(), config.getUrl());
    	if (!flag) {
    	   ResponseUtils.responseCustomMsg(response, "链接填写错误");
    	} else {
    		String execTime = config.getExecTime();
    		String url = config.getUrl();
    		if(StringUtils.isNotBlank(url)){
    			url=url.trim().replace("\n","").replace("\r","");
    		}
    		config.setExecDay(execTime.substring(0, 10).replace("-", ""));
    		brushConfigTimerDAO.updateConfig(config);
    		ResponseUtils.responseSuccess(response);	
    	}
	}

	@RequestMapping(value = "/deleteConfig")
	public void deleteRetent(Integer id, HttpServletResponse response) {
		brushConfigTimerDAO.deleteConfig(id);
		ResponseUtils.responseSuccess(response);
	}


	@RequestMapping(value = "{id}/configCopy")
	public String configCopy(BrushConfigTimer config, Model model) {
		BrushConfigTimer tempConfig = brushConfigTimerDAO.selectConfig(config);
		tempConfig.setId(null);
		model.addAttribute("config", tempConfig);
		return "brushConfigTimer/copyConfig";
	}
}
